import {createRouter,createWebHistory} from 'vue-router'
import {ref} from 'vue'
import {useUserStore} from "@/stores/user.js";
import { ElNotification } from 'element-plus'

const limitRouters=ref([
    "userCenter",
    "myShop"
])

const router = createRouter({
    history:createWebHistory(),
    routes: [
        {
            //网页入口
            path:'/',
            name:'entrance',
            component:() => import('@/views/Application.vue'),
            redirect:'homePage',//默认为首页
            children:[//二级路由
                //首页
                {path:'/homePage',name:'homePage',component:() =>import('@/views/HomePage/HomePage.vue')},
                //供应大厅
                {path:'/supplyHall',name:'supplyHall',component:() =>import('@/views/SupplyHall/SupplyHall.vue')},
                //采购大厅
                {path:'/procurementHall',name:'procurementHall',component:() =>import('@/views/ProcurementHall/ProcurementHall.vue')},
                //客服页面
                {path:'/customer-service',name:'customerService',component:() =>import('@/views/CustomerService/CustomerServicePage.vue')},

            ]
        },
        {
            //个人中心
            path:'/userCenter',
            name:'userCenter',
            component:() =>import('@/views/UserCenter/UserCenterPage.vue'),
            redirect:'/userCenter/home',//默认为首页
            children:[//二级路由
                //首页
                {path:'home',name:'home',component:() =>import('@/views/UserCenter/UC_home.vue')},
                //购物车
               {path:'cart',name:'cart',component:() =>import('@/views/UserCenter/UC_cart.vue')},
                //订单
                {path:'/userCenter/order',name:'myorder',component:() =>import('@/views/UserCenter/UC_order.vue')},
                //收藏
                {path:'favorites',name:'favorites',component:() =>import('@/views/UserCenter/UC_favorites.vue')},
                //修改密码
                {path:'/userCenter/cpwd',name:'cpwd',component:() =>import('@/views/UserCenter/UC_changePwd.vue')},
                //个人资料
                {path:'/userCenter/info',name:'userinfo',component:() =>import('@/views/UserCenter/UC_info.vue')},
                //收货地址
                {path:'/userCenter/address',name:'address',component:() =>import('@/views/UserCenter/UC_address.vue')},
                //开店
                {path:'/userCenter/open',name:'openShop',component:() =>import('@/views/UserCenter/UC_openShop.vue')},
                //专家认证
                {path:'/userCenter/expert',name:'authExpert',component:() =>import('@/views/UserCenter/UC_expert.vue')},

            ]
        },
        {
            //我的店铺
            path:'/myShop',
            name:'myShop',
            component:() =>import('@/views/shop/shopPage.vue'),
            redirect:'/myShop/info',
            children:[//二级路由
                //店铺资料
                {path:'/myShop/info',name:'myShopInfo',component:() =>import('@/views/shop/shop_info.vue')},
                //数据统计
                {path:'/myShop/data',name:'data',component:() =>import('@/views/shop/shop_data.vue')},
                //订单列表
                {path:'/myShop/order',name:'shopOrder',component:() =>import('@/views/shop/shop_order.vue')},
                //商品列表
                {path:'/myShop/goods',name:'goods',component:() =>import('@/views/shop/shop_goodsList.vue')},
                //发布商品
                {path:'/myShop/release',name:'release',component:() =>import('@/views/shop/shop_release.vue')},
                //客服管理
                {path:'/myShop/service',name:'service',component:() =>import('@/views/shop/shop_service.vue')},

            ]
        },
        {path:'/expertHomePage',name:'expertHomePage',component:() =>import('@//views/Expert/ExpertHomePage.vue')},
        {path:'/expert/detail/:uid',name:'expertDetailed',component:()=>import('@/views/Expert/ExpertDetail.vue')},
        {path:'/expert/newArticle',name:'expertNewArticle',component:() =>import('@/views/Expert/ExpertPostArticle.vue') },
        {path:'/expert/questionPage',name:'expertQuestionPage',component:() =>import('@/views/Expert/ExpertQuestion.vue')},
        {path:'/expert/articleDetail/:aid',name:'articleDetail',component:() =>import('@/views/Expert/articleDetail.vue')},
        {path:'/question/:qid',name:'questionDetail',component:() =>import('@/views/Expert/QuestionDetail.vue')},
        {
            path:'/expertManager',
            name:'expertManager',
            component:()=>import('@/views/Expert/ExpertManager.vue'),
            children:[
                {path: '/expertManager/info',name:'ExpertInfo',component:() =>import('@/views/Expert/ExpertManager_Info.vue')},
            ]
        },

        { //融资项目板块
            path:'/finance',
            name:'Finance',
            redirect:'/finance/project',
            component:() =>import('@/views/Finance/FinanceApplicationPage.vue'),
            children:[
                //项目管理
                {path:'project',name:'project',component:() => import('@/views/Finance/FA_project.vue')},
                //项目进程
                {path:'progress',name:'progress',component:() =>import('@/views/Finance/FA_progress.vue')},

                //融资申请表
                {path:'form',name:'form',component:() =>import('@/views/Finance/FA_form.vue')},
                //融资用户信息资料
                {path:'info',name:'info',component:() =>import('@/views/Finance/FA_info.vue')},
                //信誉等级
                {path:'credit',name:'credit',component:() =>import('@/views/Finance/FA_credit.vue')},
                //收入证明
                {path:'income',name:'income',component:() =>import('@/views/Finance/FA_income.vue')},
                //财产证明
                {path:'property',name:'property',component:() =>import('@/views/Finance/FA_property.vue')},
                //其他
                {path:'other',name:'other',component:() =>import('@/views/Finance/FA_other.vue')},

                {path:'test120',name:'test120',component:() =>import('@/views/Finance/TestPage.vue')},
            ]

        },
        // 管理员审核相关路由配置
        {
            path: '/admin',
            name: 'Admin',
            redirect: '/admin/dashboard',
            component: () => import('@/views/Admin/AdminFinanceReviewPage.vue'), // 使用布局组件
            children: [
                {
                    path: 'dashboard',
                    name: 'AdminDashboard',
                    component: () => import('@/views/Admin/Dashboard.vue') // 仪表盘页面
                },
                {
                    path: 'users',
                    name: 'UserManagement',
                    component: () => import('@/views/Admin/UserManagement.vue') // 用户管理页面
                },
                {
                    path: 'finance/review',
                    name: 'FinanceReview',
                    component: () => import('@/views/Admin/FA_review.vue') // 审核页面
                },
                {
                    path: 'goods',
                    name: 'GoodsManagement',
                    component: () => import('@/views/Admin/GoodsManagement.vue') // 商品管理页面
                }
                // 可以继续添加其他管理员子路由
            ],
            // 管理员路由守卫
            beforeEnter: (to, from, next) => {
                const userStore = useUserStore();
                if (userStore.isLogin && userStore.isAdmin) {
                    next();
                } else {
                    ElNotification({
                        title: '权限不足',
                        message: '仅管理员可访问此页面',
                        type: 'warning'
                    });
                    next(from.path !== '/admin' ? from.path : '/homePage');
                }
            }
        },

        {path:'/test',name:'Test',component:() =>import('@/views/Test.vue')},
        {path:'/login',name:'Login',component:() =>import('@/views/Login/LoginPage.vue')},
        {path:'/register',name:'register',component:() =>import('@/views/Login/RegisterPage.vue')},
        {path:'/category/:categoryName',name:'category',component:() =>import('@/views/Common/searchResult.vue')},
        {path:'/detail/:id',name:'detail',component:() =>import('@/views/Detail/goodsDetail.vue')},
        {path:'/shop/:id',name:'shop',component:() =>import('@/views/shop/shop.vue')},
        //订单页面
        {path:'/order/:goods',name:'userOrder',component:() =>import('@/views/Detail/orderDetail.vue')},
        //支付页面
        {path:'/pay',name:'pay',component:() =>import('@/views/Common/Payment.vue')},
        // 不识别的path自动匹配404
        {path:'/:catchAll(.*)',name:'NotFind',component:() =>import('@/views/Common/NotFind.vue')},

    ]
})
//路由守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore();

    // 检查是否需要登录
    if (to.path.startsWith('/userCenter')) {
        if (userStore.isLogin) {
            next();
        } else {
            ElNotification({
                title: '提示',
                message: '请先登录',
                type: 'warning'
            });
            next('/login');
        }
        return; // 结束守卫
    }

    // 检查是否需要开店
    if (to.path.startsWith('/myShop')) {
        if (!userStore.isLogin) {
             ElNotification({
                title: '提示',
                message: '请先登录',
                type: 'warning'
            });
            next('/login');
        }
        else if (userStore.myShop) {
            next();
        } else {
            ElNotification({
                title: '提示',
                message: '您当前未开店，即将前往开店页面',
                type: 'warning'
            });
            next('/userCenter/open');
        }
        return; // 结束守卫
    }

    // 如果以上都不是，直接放行
    next();
});

export default router